package com.aiyi.education.user.controller;


import com.aiyi.education.entity.KsUser;
import com.aiyi.education.entity.KsUserGroup;
import com.aiyi.education.entity.KsUserGroupWithBLOBs;
import com.aiyi.education.entity.KsUserWithBLOBs;
import com.aiyi.education.user.service.IKsUserGroupService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.annotation.Pass;
import com.education.util.vo.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * <p>
 * 用户组 前端控制器
 * </p>
 *
 * @author renyong
 * @since 2020-04-22
 */
@Slf4j
@RestController
@Api(tags = "用户组管理")
@RequestMapping("/user/ks-user-group")
public class KsUserGroupController {

    @Autowired
    private IKsUserGroupService iKsUserGroupService;

    @Pass
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public Result<IPage<KsUserGroupWithBLOBs>>  queryListUserGroup(KsUserGroupWithBLOBs KsUserGroupWithBLOBs, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req){
        Result<IPage<KsUserGroupWithBLOBs>> result = new Result<>();
        QueryWrapper<KsUserGroupWithBLOBs> queryWrapper = new QueryWrapper<>();
        Page<KsUserGroupWithBLOBs> page = new Page<>(pageNo, pageSize);
        String groupName=req.getParameter("groupName");
        if(!StringUtils.isEmpty(groupName)){
            queryWrapper.eq("group_name",groupName);
        }
        IPage<KsUserGroupWithBLOBs> pageList = iKsUserGroupService.page(page,queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        log.info(pageList.toString());
        return result;
    }


    /**
     * 添加用户组
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<KsUserGroupWithBLOBs>  addUser(@RequestBody JSONObject jsonObject){
        Result<KsUserGroupWithBLOBs> result = new Result<KsUserGroupWithBLOBs>();
        try{
            KsUserGroupWithBLOBs ksUserGroup= JSON.parseObject(jsonObject.toJSONString(), KsUserGroupWithBLOBs.class);
            iKsUserGroupService.save(ksUserGroup);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 删除用户组
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        iKsUserGroupService.removeById(Integer.parseInt(id));
        return Result.ok("删除用户组成功");
    }

    /**
     * 批量删除用户组
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        iKsUserGroupService.deleteBatchUsers(ids);
        return Result.ok("批量删除用户组成功");
    }

    /**
     * 修改用户组
     * @param ksUserGroupWithBLOBs
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Result<?> edit(@RequestBody KsUserGroupWithBLOBs ksUserGroupWithBLOBs ){
        iKsUserGroupService.updateById(ksUserGroupWithBLOBs);
        return Result.ok("用户组修改成功");
    }

}
